package com.wy.mytextview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * Time:7/26/21
 * Author:ypf
 * Description:
 */
public class TextView extends View {

    private String TAG = "TextView";

    private Paint mPaint;

    private int mTextColor;
    private int mTextSize;
    private String mText;
    private int mInputType;

    public TextView(Context context) {
        this(context, null, 0);
    }

    public TextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TextView, defStyleAttr, 0);
        mTextColor = typedArray.getColor(R.styleable.TextView_darrenTextColor, Color.RED);
        mTextSize = typedArray.getDimensionPixelSize(R.styleable.TextView_darrenTextSize, 15);
        mText = typedArray.getString(R.styleable.TextView_darrenText);
        mInputType = typedArray.getInteger(R.styleable.TextView_darrenInputType, 1);
        typedArray.recycle();
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(mTextColor);
        mPaint.setTextSize(mTextSize);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //布局模式
        int widthMeasureSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMeasureSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        Log.i(TAG, "widthMeasureSpecMode:" + widthMeasureSpecMode + ",heightMeasureSpecMode:" + heightMeasureSpecMode);

        //布局大小
        int widthMeasureSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMeasureSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        Log.i(TAG, "widthMeasureSpecSize:" + widthMeasureSpecSize + ",heightMeasureSpecSize:" + heightMeasureSpecSize);

        //处理wrap_content
        if (widthMeasureSpecMode == MeasureSpec.AT_MOST) {
            Log.i(TAG, "MeasureSpec.AT_MOST:" + MeasureSpec.AT_MOST);
            Rect bounds = new Rect();
            mPaint.getTextBounds(mText, 0, mText.length(), bounds);
            widthMeasureSpecSize = bounds.width();
        } else if (widthMeasureSpecMode == MeasureSpec.EXACTLY) {
            Log.i(TAG, "MeasureSpec.EXACTLY:" + MeasureSpec.EXACTLY);
        } else if (widthMeasureSpecMode == MeasureSpec.UNSPECIFIED) {
            Log.i(TAG, "MeasureSpec.UNSPECIFIED:" + MeasureSpec.UNSPECIFIED);
        }

        //处理wrap_content
        if (widthMeasureSpecMode == MeasureSpec.AT_MOST) {
            Log.i(TAG, "MeasureSpec.AT_MOST:" + MeasureSpec.AT_MOST);
            Rect bounds = new Rect();
            mPaint.getTextBounds(mText, 0, mText.length(), bounds);
            heightMeasureSpecSize = bounds.height();
        } else if (widthMeasureSpecMode == MeasureSpec.EXACTLY) {
            Log.i(TAG, "MeasureSpec.EXACTLY:" + MeasureSpec.EXACTLY);
        } else if (widthMeasureSpecMode == MeasureSpec.UNSPECIFIED) {
            Log.i(TAG, "MeasureSpec.UNSPECIFIED:" + MeasureSpec.UNSPECIFIED);
        }

        //处理padding
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        Log.i(TAG, "paddingLeft:" + paddingLeft + ",paddingRight:" + paddingRight + ",paddingTop:" + paddingTop + ",paddingBottom:" + paddingBottom);

        //设置测量后的宽高
        setMeasuredDimension(paddingLeft + widthMeasureSpecSize + paddingRight, paddingTop + heightMeasureSpecSize + paddingBottom);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //计算基线的坐标
        Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
        int leading = fontMetrics.leading;
        int ascent = fontMetrics.ascent;
        int descent = fontMetrics.descent;
        int top = fontMetrics.top;
        int bottom = fontMetrics.bottom;
        Log.i(TAG, "leading:" + leading + ",ascent:" + ascent + ",descent:" + descent + ",top:" + top + ",bottom:" + bottom);
        Log.i(TAG, "width:" + getWidth() + ",height:" + getHeight());

        int dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        int baseLine = getHeight() / 2 + dy;
        Log.i(TAG, "dy:" + dy + ", baseLine:" + baseLine);

        //处理padding
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        Log.i(TAG, "paddingLeft:" + paddingLeft + ",paddingRight:" + paddingRight + ",paddingTop:" + paddingTop + ",paddingBottom:" + paddingBottom);

        //绘制文字
        canvas.drawText(mText, paddingLeft + 0, baseLine, mPaint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, "按下");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i(TAG, "移动");
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG, "抬起");
                break;
        }
        return true;
    }

}
